<?php $this->pageHeader('Generell statistikk'); ?>
<a href="<?php echo $this->url(array('action' => 'index')) ?>" class="button">Tilbake</a>
<p class="clear"></p>


<?php 
    $this->filterForm->setMethod('get');
    $this->filterForm->setAction($this->url());
    echo $this->filterForm;

    $totalShifts = 0;
    $totalWorkHours = 0;
    foreach ($this->totalHoursAndShifts as $row)
    {
        $totalShifts += $row['number_of_shifts'];
        $totalWorkHours += $row['hours'];
    }
?>

<table>
    <thead>
        <tr>
            <th>Måned</th>
            <th>Antall vakter</th>
            <th>Antall arbeidstimer</th>
        </tr>
        <tr>
            <td><strong>Totalt</strong></td>
            <td><strong><?php echo $this->escape($totalShifts); ?></strong></td>
            <td><strong><?php echo $this->escape((int)$totalWorkHours); ?></strong></td>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($this->totalHoursAndShifts as $row ) :
        $date = new RedCross_Date($row['year'] . '-' . $row['month'],'YYYY-MM');

        // If there is no shifts in a whole month, there will be no row in the Doctrine result for that month.
        // The list will therefore by default write only months that have shifts and skip the ones without shifts
        // We therefore have to check if we skip one or more months and then add rows with 0-values manually
        if (isset($lastDate))
        {
            $tempDate = new RedCross_Date($lastDate);
            $tempDate->add(1, RedCross_Date::MONTH);
            while ($tempDate->isEarlier($date))
            {
                $dateStr = $tempDate->get(RedCross_Date::MONTH_NAME) . ', ' . $tempDate->get(RedCross_Date::YEAR);
                echo '<tr><td>' . $dateStr . '</td><td>0</td><td>0</td></tr>';
                $tempDate->add(1, RedCross_Date::MONTH);
            }
        }

        // Write the actual row with data
        $dateStr = $date->get(RedCross_Date::MONTH_NAME) . ', ' . $date->get(RedCross_Date::YEAR);
        $lastDate = new RedCross_Date($date);
    ?>
        <tr>
            <td><?php echo $this->escape($dateStr); ?></td>
            <td><?php echo $this->escape($row['number_of_shifts']); ?></td>
            <td><?php echo $this->escape((int)$row['hours']); ?></td>
        </tr>
    <?php endforeach; ?>
        <tr>
            <td><strong>Totalt</strong></td>
            <td><strong><?php echo $this->escape($totalShifts); ?></strong></td>
            <td><strong><?php echo $this->escape((int)$totalWorkHours); ?></strong></td>
        </tr>
    </tbody>
</table>
